package com.oo.parkinglot;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ParkingLot {

    private String parkingLotName;

    private int capacity;

    private Map<String, Car> cars = new HashMap<>();

    public ParkingLot(int capacity) {
        this.capacity = capacity;
    }


    public ParkingLot(String parkingLotName, int capacity) {
        this (capacity);
        this.parkingLotName = parkingLotName;
    }



    public int getAvailable() {
        return this.capacity - cars.size();
    }

    public int occupy() {
        return cars.size();
    }

    public boolean park(Car car) {
        if (cars.containsKey(car.getCarNum()) || getAvailable() == 0) {
            return false;
        }
        doPark(car);
        return true;
    }

    protected void doPark(Car car) {
        cars.put(car.getCarNum(), car);
    }

    public Car pickUp(String carNum) {
        Car car = cars.get(carNum);
        if (!cars.containsKey(carNum)) {
            return null;
        }
        doPickUp(car);
        return car;
    }

    protected void doPickUp(Car car) {
        cars.remove(car.getCarNum());
    }

    protected Map<String, Car> getCars() {
        return this.cars;
    }

    public String getParkingLotName() {
        return this.parkingLotName;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof ParkingLot) {
            ParkingLot car = (ParkingLot) obj;
            if (this.parkingLotName.equals(car.getParkingLotName())) {
                return true;
            }
        }
        return false;
    }

}
